package application;

import application.dao.DBService;
import application.dao.entity.Manager;
import application.dao.entity.User;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;


public class Login_controller extends BaseController {

    @FXML
    public VBox operateBox;
    javafx.stage.Stage stage;
    @FXML
    private PasswordField passWord;

    @FXML
    private RadioButton woman;

    @FXML
    private Button register_register;

    @FXML
    private ImageView userPic;

    @FXML
    private TextField IDcard_register;

    @FXML
    private Button login;

    @FXML
    private RadioButton Athlete;

    @FXML
    private Button login_register;

    @FXML
    private ImageView controlPic;

    @FXML
    private VBox controlPerson;

    @FXML
    private ImageView athletePic;

    @FXML
    private TextField userName_register;

    @FXML
    private ToggleGroup Registered_identity;

    @FXML
    private RadioButton man;

    @FXML
    private RadioButton clPerson;

    @FXML
    private VBox athlete;

    @FXML
    private VBox regAthlete;
    @FXML
    private VBox loginBox;

    @FXML
    private TextField realName_register;

    @FXML
    private TextField telNumber_register;

    @FXML
    private Button submit_register;

    @FXML
    private ToggleGroup sex;

    @FXML
    private TextField userName;

    @FXML
    private ImageView passPic;

    @FXML
    private ImageView background;

    @FXML
    private PasswordField passWord_register;

    @FXML
    private Button register;

    @FXML
    void onButtonLogin(ActionEvent event) throws Exception {
        showLogin();
    }

    private void showLogin() {
        operateBox.setVisible(false);
        regAthlete.setVisible(false);
        loginBox.setVisible(true);
    }

    private void doLogin() {
        boolean athlete = man.isSelected();
        boolean admin = clPerson.isSelected();
        if (athlete != true && admin != true) {
            BaseUtil.info("请选择角色再登录");
            return;
        }
        String user = userName.getText();
        String text = passWord.getText();
        if (admin) {
            Manager loginUser = DBService.loginAdmin(user, text);
            if (null == loginUser) {
                BaseUtil.err("管理员账号/密码错误");
                return;
            }
            stage.close();
            Manager_main_show main = new Manager_main_show();
            Global.loginAdmin = loginUser;
            BaseUtil.goPage(main, main.stage);
        } else {
            User loginUser = DBService.loginUser(user, text);
            if (null == loginUser) {
                BaseUtil.err("运动员账号/密码错误");
                return;
            }
            stage.close();
            Athlete_main_show show = new Athlete_main_show();
            Global.loginUser = loginUser;
            BaseUtil.goPage(show, show.stage);
        }
    }

    @FXML
    void onButtonRegister(ActionEvent event) {
        showRegister();
    }

    private void showRegister() {
        operateBox.setVisible(false);
        loginBox.setVisible(false);
        regAthlete.setVisible(true);
    }

    @FXML
    void inputUserName(ActionEvent event) {

    }

    @FXML
    void Login_register(MouseEvent event) {
        clPerson.setSelected(true);
        man.setSelected(false);
        doLogin();
    }

    @FXML
    void Login_athlete(MouseEvent event) {
        man.setSelected(true);
        clPerson.setSelected(false);
        doLogin();
    }

    @FXML
    void inputUserName_register(ActionEvent event) {

    }

    @FXML
    void inputPassWord_register(ActionEvent event) {

    }

    @FXML
    void inputRealName_register(ActionEvent event) {

    }

    @FXML
    void inputIDcard(ActionEvent event) {

    }

    @FXML
    void inputTelNumber(ActionEvent event) {

    }

    @FXML
    void onButtonSubmit(ActionEvent event) throws InterruptedException {
        boolean admin = clPerson.isSelected();
        boolean b = false;
        if (admin) {
            Manager user = getAdminByLabel();
            if (user.validate()) {
                BaseUtil.info("注册失败");
                return;
            }
            b = DBService.addManager(user);
        } else {
            User user = getUserByLabel();
            if (user.validate()) {
                BaseUtil.info("注册失败");
                return;
            }
            b = DBService.addUser(user);
        }
        if (b) {
            BaseUtil.info("注册成功");
            Thread.sleep(2000);
        }
        Login_show application = new Login_show();
        stage.close();
        BaseUtil.hideGoPage(stage, application, application.stage);
    }

    private Manager getAdminByLabel() {
        Manager manager = new Manager();
        manager.setIdCard(IDcard_register.getText());
        manager.setPassWord(passWord_register.getText());
        manager.setRealName(realName_register.getText());

        manager.setSex(woman.isSelected() ? "女" : "男");
        manager.setTelNumber(telNumber_register.getText());
        manager.setUserName(userName_register.getText());
        return manager;
    }

    private User getUserByLabel() {
        User manager = new User();
        manager.setIdCard(IDcard_register.getText());
        manager.setPassWord(passWord_register.getText());
        manager.setRealName(realName_register.getText());

        manager.setSex(woman.isSelected() ? "女" : "男");
        manager.setTelNumber(telNumber_register.getText());
        manager.setUserName(userName_register.getText());
        return manager;
    }

    @FXML
    void onButtonLogin_register(ActionEvent event) throws Exception {

    }

    @FXML
    void onButtonRegister_register(ActionEvent event) {

    }

    public void setApp(javafx.stage.Stage stage) {
        this.stage = stage;
    }


    @Override
    public void init() {

    }
}
